package com.daka.service.impl;

import com.daka.dao.UserDao;
import com.daka.dao1.MemberDao;
import com.daka.entity.Member;
import com.daka.entity.User;
import com.daka.service.UserService;
import com.daka.tools.ConfigUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:24
 */
@Service("userService")
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private MemberDao memberDao;

    /**
     * 数据迁移函数
     *
     * @param max 迁移老数据库中member表Id最大值
     * @param min 迁移老数据库中member表Id最小值
     */
    public void migrate(Integer max, Integer min) {

        Integer seed = 10000;

        List<Member> memberList;
        if (max == 0 || min == 0 || max - min < seed) {
            memberList = memberDao.selectAll();
            insertBatch(memberList);
        } else {
            int index = max / seed;

            for (int i = 1; i <= index + 1; i++) {

                int pMin = (seed * (i - 1)) + min;
                int pMax = (seed * (i - 1)) + seed + min - 1;
                log.info("User 第{" + i + "}批，" + pMin + " 到" + pMax + "条");

                memberList = memberDao.selectByIdBetween(pMax, pMin);
                insertBatch(memberList);
            }

        }

    }

    public void correction() {
        List<String> mobileList = userDao.selectByMobileGroup();
        User newUser = null;
        for (String mobile : mobileList) {
            List<User> userList = userDao.selectByMobile(mobile);
            User tempUser = null;
            for (User user : userList) {
                if (user.getOldid() == 0) {
                    newUser = user;
                } else {
                    if (tempUser == null) {
                        tempUser = user;
                    } else {
                        if (tempUser.getCreateTime().getTime() > user.getCreateTime().getTime()) {
                            userDao.deleteByPrimaryKey(user.getId());
                        } else {
                            userDao.deleteByPrimaryKey(tempUser.getId());
                            tempUser = user;
                        }
                    }
                }
            }

            if (null != newUser && tempUser != null) {
                newUser.setOldid(tempUser.getOldid());
                newUser.setOldMemberCode(tempUser.getOldMemberCode());

                userDao.deleteByPrimaryKey(tempUser.getId());

                userDao.updateByPrimaryKeySelective(newUser);
            }
        }


        List<String> eMailList = userDao.selectByEmailGroup();
        for (String eMail : eMailList) {
            List<User> userList = userDao.selectByEmail(eMail);
            User tempUser = null;
            for (User user : userList) {
                if (user.getOldid() == 0) {
                    newUser = user;
                } else {
                    if (tempUser == null) {
                        tempUser = user;
                    } else {
                        if (tempUser.getCreateTime().getTime() > user.getCreateTime().getTime()) {
                            userDao.deleteByPrimaryKey(user.getId());
                        } else {
                            userDao.deleteByPrimaryKey(tempUser.getId());
                            tempUser = user;
                        }
                    }
                }
            }

            if (null != newUser && tempUser != null) {
                newUser.setOldid(tempUser.getOldid());
                newUser.setOldMemberCode(tempUser.getOldMemberCode());

                userDao.deleteByPrimaryKey(tempUser.getId());

                userDao.updateByPrimaryKeySelective(newUser);
            }
        }
    }

    private void insertBatch(List<Member> memberList) {
        List<User> userList = new ArrayList<User>();
        for (Member member : memberList) {

            userList.add(User.build(member));

            if (userList.size() >= ConfigUtil.BATCH_INSERT_MAX) {
                int i = userDao.insertBatch(userList);

                if (i > 0) {
                    userList.clear();
                }
            }
        }

        if (userList.size() > 0) {
            userDao.insertBatch(userList);
        }
    }

    /**
     * 根据老数据库member表ID在新库中查询对应的新库UserId
     *
     * @param oldId 老库中member表Id
     * @return 新库中User对象
     */
    public User findByOldId(Long oldId) {
        return userDao.selectByOldId(oldId);
    }
}
